package com.photohistory.hibernate.dao;
// Generated 21.03.2010 16:27:53 by Hibernate Tools 3.2.4.GA


import com.photohistory.hibernate.beans.Comment;

import java.util.List;
import java.util.Set;

import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import static org.hibernate.criterion.Example.create;

/**
 * Home object for domain model class Comment.
 * @see com.photohistory.hibernate.dao.Comment
 * @author Hibernate Tools
 */
public class CommentHome {
	
	private static final Log log = LogFactory.getLog(CategoryHome.class);
	private final SessionFactory sessionFactory = HibernateSessionFactory.getSessionFactory();

		
	public Comment get(Integer id) {
			
		Comment comment = null;			
		try {
				
			Session session = HibernateSessionFactory.currentSession();				
			comment = (Comment) session.load(Comment.class, id);				
			
		} catch (HibernateException e) {				
			throw new RuntimeException(e.getMessage());			
		} finally {				
			// HibernateSessionFactory.closeSession();			
		}
			
		return comment;		
	}
		
		public List getAll() {
			List list = null;
			try {
				Session session = HibernateSessionFactory.currentSession();
				list = session.createQuery("from " + Comment.class.getName())
						.list();
			} catch (HibernateException e) {
				// throw new RuntimeException(e.getMessage());
				throw e;
			} finally {
				// HibernateSessionFactory.closeSession();
			}
			return list;
		}
		
		public void save(Comment comment) {
			try {
				Session session = HibernateSessionFactory.currentSession();
				HibernateSessionFactory.beginTransaction();
				session.saveOrUpdate(comment);
				HibernateSessionFactory.commitTransaction();
			} catch (HibernateException e) {
				HibernateSessionFactory.rollbackTransaction();
				throw new RuntimeException(e.getMessage());
			} finally {
				// HibernateSessionFactory.closeSession();
			}

		}

		public void remove(Comment comment) {
			try {
				Session session = HibernateSessionFactory.currentSession();
				HibernateSessionFactory.beginTransaction();
				session.delete(comment);
				HibernateSessionFactory.commitTransaction();
				session.flush();
			} catch (Exception e) {
				HibernateSessionFactory.rollbackTransaction();
				throw new RuntimeException(e.getMessage());
			} finally {
				// HibernateSessionFactory.closeSession();
			}
		}

		public List find(String where) {
			List list = null;
			try {
				Session session = HibernateSessionFactory.currentSession();
				list = session.createQuery(
						" from Comment "  + " where " + where)
						.list();
			} catch (HibernateException e) {
				throw new RuntimeException(e.getMessage());
			} finally {
				// HibernateSessionFactory.closeSession();
			}

			return list;
		}

		public Set initSet(Set set) {
			try {
				Hibernate.initialize(set);
			} catch (Exception e) {
				throw new RuntimeException(e.getMessage());
			} finally {
				// HibernateSessionFactory.closeSession();
			}
			return set;
		}
	    
}

